<script setup lang="ts">
import type { FormRules } from "element-plus"
import type { RegisterRequestData } from "./apis/type"
import { useUserStore } from "@/pinia/stores/user"
import { registerApi } from "./apis"

const router = useRouter()

const userStore = useUserStore()

/** 登录表单元素的引用 */
const registerFormRef = useTemplateRef("registerFormRef")

/** 登录按钮 Loading */
const loading = ref(false)

/** 登录表单数据 */
const registerFormData: RegisterRequestData = reactive({
  username: "",
  account: "",
  password: "",
  passwordConfirm: "",
  mobile: "",
  code: ""
})

/** 登录表单校验规则 */
const loginFormRules: FormRules = {
  account: [
    { required: true, message: "请输入手机号码", trigger: "blur" },
    { min: 11, max: 11, message: "请输入正确的手机号码", trigger: "blur" },
    // 判断手机号码格式
    {
      validator: (rule, value, callback) => {
        if (!value) {
          callback(new Error("请输入手机号码"))
        } else if (!/^1[3456789]\d{9}$/.test(value)) {
          callback(new Error("请输入正确的手机号码"))
        } else {
          callback()
        }
      }
    }
  ],
  password: [
    { required: true, message: "请输入密码", trigger: "blur" },
    { min: 6, max: 16, message: "长度在 6 到 16 个字符", trigger: "blur" }
  ],
  passwordConfirm: [
    { required: true, message: "请输入确认密码", trigger: "blur" },
    { min: 6, max: 16, message: "长度在 6 到 16 个字符", trigger: "blur" }
  ],
  code: [
    { required: true, message: "请输入验证码", trigger: "blur" }
  ]
}
/** 登录 */
function handleRegister() {
  registerFormRef.value?.validate((valid) => {
    if (!valid) {
      return
    }
    if (registerFormData.passwordConfirm !== registerFormData.password) {
      ElMessage.error("两次输入的密码不一致！")
      return
    }
    loading.value = true
    registerFormData.mobile = ""
    registerFormData.username = registerFormData.account
    registerFormData.code = "123456"
    registerApi(registerFormData).then((data) => {
      console.log(data)
      if (data.code === 0) {
        userStore.setUsername(registerFormData.account)
        userStore.setToken(data.data.token) // token-admin
        userStore.setUserId(data.data.id) // token-admin
        ElMessage.success({
          message: "注册成功！",
          type: "success",
          duration: 3000
        })
        setTimeout(() => {
          handleLogin()
        }, 3000)
      } else {
        ElMessage.error({
          message: data.msg,
          type: "warning",
          duration: 3000
        })
      }
    }).catch(() => {
      ElMessage.error({
        message: `注册失败！！`,
        type: "warning",
        duration: 3000
      })
      // registerFormData.password = ""
    }).finally(() => {
      loading.value = false
    })
  })
}
function handleLogin() {
  router.push("/")
}
</script>

<template>
  <div class="login-container">
    <div class="content">
      <div class="tab-nav">
        <div class="tab-item current">
          注册账号
        </div>
      </div>
      <el-form ref="registerFormRef" :model="registerFormData" :rules="loginFormRules" @keyup.enter="handleRegister">
        <div class="form-content">
          <el-form-item prop="account">
            <div class="form-item">
              <label for="id1" class="account" />
              <input id="id1" v-model.trim="registerFormData.account" autocomplete="false" type="text" placeholder="请输入手机号码">
            </div>
          </el-form-item>
          <el-form-item prop="password">
            <div class="form-item">
              <label for="id3" class="password" />
              <input id="id3" type="password" autocomplete="false"  v-model.trim="registerFormData.password" placeholder="请输入密码">
            </div>
          </el-form-item>
          <el-form-item prop="passwordConfirm">
            <div class="form-item">
              <label for="id4" class="password" />
              <input id="id4" type="password" autocomplete="false"  v-model.trim="registerFormData.passwordConfirm" placeholder="再次请输入密码">
            </div>
          </el-form-item>
          <button type="button" class="submit" @click.prevent="handleRegister">
            注册
          </button>
          <div class="tip">
            <div class="hasaccount">
              <span>还没有账号？</span>
              <a href="/#/login" class=""><span class="goLogin">立即登录</span></a>
            </div>
            <div class="forget">
              <a href="/#/" class=""><span class="forget-text-home">返回首页</span></a>
            </div>
          </div>
        </div>
      </el-form>
    </div>
  </div>
</template>

<style>
.el-form-item {
  margin-bottom: 0;
}
.login-container {
  width: 100%;
  height: 100%;
  background-position: 0 0;
  background-repeat: no-repeat;
  background-size: 100% 100%;
  display: flex;
  align-items: center;
  justify-content: center;
}
.content {
  width: 736px;
  height: 688px;
  background: #ffffff;
  border-radius: 30px;
  overflow: hidden;
}
.tab-nav {
  margin-top: 80px;
  display: flex;
  justify-content: center;
}

.form-item {
  width: 480px;
  height: 60px;
  background: rgba(0, 0, 0, 0.05);
  border-radius: 5px;
  margin: 0 auto 40px;
  display: flex;
  align-items: center;
  padding: 0 22px;
  box-sizing: border-box;
  position: relative;
}

.agree {
  width: 410px;
  height: 20px;
  margin: 0 auto;
  cursor: pointer;
  font-size: 18px;
  color: #333;
}

.tip {
  width: 410px;
  margin: 0 auto;
  display: flex;
  justify-content: space-between;
}
.current {
  color: #45a1ff;
  margin-right: 100px;
}

.submit {
  display: block;
  width: 410px;
  height: 60px;
  line-height: 60px;
  background: #45a1ff;
  border-radius: 5px;
  font-family: xiaomi;
  font-size: 22px;
  color: #fff;
  letter-spacing: 20px;
  text-align: center;
  border: none;
  margin: 10px auto 10px;
  cursor: pointer;
}
.account {
  display: block;
  width: 17px;
  height: 25px;
  background: url()
    no-repeat center center / 100%;
}
.form-item input {
  height: 25px;
  width: 400px;
  margin-left: 22px;
  border: none;
  background-color: transparent;
  padding-left: 20px;
  border-left: 1px solid rgba(0, 0, 0, 0.3);
  box-sizing: border-box;
  outline: none;
}
.password {
  display: block;
  width: 20px;
  height: 25px;
  background: url()
    no-repeat center center / 100%;
}
.tab-item {
  font-family: xiaomi;
  color: #333;
  font-size: 28px;
  line-height: 37px;
  text-align: center;
  cursor: pointer;
}
.tab-nav {
  margin-top: 80px;
  display: flex;
  justify-content: center;
}
.current {
  color: #45a1ff;
  margin-right: 100px;
}
.form-content {
  margin-top: 60px;
}
.tip .goLogin {
  color: #45a1ff;
}
.forget .forget-text-home {
  color: #45a1ff;
  line-height: 24px;
}
.el-checkbox__label {
  display: inline-block;
  padding-left: 8px;
  line-height: 1;
  font-size: var(--el-checkbox-font-size);
}
.el-form-item__error {
  padding-left: 190px;
  top: 70px;
  font-size: 14px;
}
</style>
